package org.dromara.quick.chat.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.utils.ip.RegionUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.web.core.BaseController;
import org.dromara.quick.chat.model.bo.create.ImSocialPostBO;
import org.dromara.quick.chat.model.bo.query.ImSocialPostQueryBO;
import org.dromara.quick.chat.service.ImSocialPostService;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: Stars
 * @description 针对表【im_social_post(社交软件中的动态表，用于存储用户发布的动态相关信息)】的数据库操作Service
 * @Date: 2025/3/6 22:33
 */
@Tag(name = "社交软件中的动态表")
@RequiredArgsConstructor
@RestController
@RequestMapping("/im/social/post")
public class ImSocialPostController extends BaseController {

    private final ImSocialPostService imSocialPostService;

    /**
     * 添加动态
     */
    @Operation(summary = "添加动态")
    @PostMapping("/add")
    public R<?> addSocialPost(@RequestBody ImSocialPostBO imSocialPostBO, HttpServletRequest request) {
        String clientIp = RegionUtils.getClientIp(request);
        String province = RegionUtils.extractProvince(clientIp);
        imSocialPostBO.setIpLocation(province);
        imSocialPostService.addSocialPost(imSocialPostBO);
        return R.ok();
    }


    /**
     * 查询朋友圈
     */
    @Operation(summary = "查询朋友圈")
    @GetMapping("/list")
    public R<?> listSocialPost(ImSocialPostQueryBO imSocialPostQueryBO) {
        return R.ok(imSocialPostService.listSocialPost(imSocialPostQueryBO));
    }

    /**
     * 查询用户朋友圈
     * <p>
     *     如果指定用户id 就根据用户id查询，没有指定就使用当前用户的id
     * </p>
     */
    @Operation(summary = "查询用户朋友圈")
    @GetMapping("/list/user")
    public R<?> listSocialPostByUserId(ImSocialPostQueryBO imSocialPostQueryBO) {
        return R.ok(imSocialPostService.listSocialPostByUserId(imSocialPostQueryBO));
    }


    /**
     * 距离有近到远搜索
     *
     * @param imSocialPostQueryBO
     * @return
     */
    @Operation(summary = "查询朋友圈-距离有近到远搜索")
    @GetMapping("/list/location")
    public R<?> findEntitiesByLocation(ImSocialPostQueryBO imSocialPostQueryBO) {
        return R.ok(imSocialPostService.findEntitiesByLocation(imSocialPostQueryBO));
    }

    /**
     * 用户点赞
     */
    @Operation(summary = "用户点赞")
    @GetMapping("/like")
    public R<?> socialLike(Long socialPostId) {
        if (socialPostId == null){
            return R.fail("参数错误");
        }
        imSocialPostService.like(socialPostId);
        return R.ok();
    }

    /**
     * 查询朋友圈明细 - 包括最近
     * @param socialPostId
     * @param pageQuery 这个是为了查询评论
     * @return
     */
    @Operation(summary = "查询朋友圈明细")
    @GetMapping("/detail/{socialPostId}")
    public R<?> detailSocialComment(@PathVariable("socialPostId") Long socialPostId, PageQuery pageQuery) {
        if (socialPostId == null){
            return R.fail("参数错误");
        }
        return R.ok(imSocialPostService.detailSocialComment(socialPostId,pageQuery));
    }
}
